'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 升级士兵
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var soldierService = require('../../../../services/soldierService');
var arenaService = require('../../../../services/arenaService');
var soldierConf = require('../../../../config/soldierConf');
var buildConf = require('../../../../config/buildConf');
var buildType = require('../../../../consts/buildType');
var PlayerManager = require('../../../../libs/playerManager');
var techType = require('../../../../consts/techType');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var soldier_type = msg.soldier_type;
    if (!soldier_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var soldier_lv_conf;
    var next_soldier_lv_conf;
    var soldier_model;
    var tech_model;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['soldier', 'arena', 'role', 'build', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            // jshint maxstatements:100
            function (cb) {
                soldier_model = player_manager.soldier;
                tech_model = player_manager.tech;
                var role_model = player_manager.role;
                var build_model = player_manager.build;

                var soldier = soldier_model.getSoldier(soldier_type);
                if (!soldier) {
                    return next(null, {
                        code: Code.SOLDIER_NOT_EXIST
                    });
                }

                // 获取兵种等级配置
                soldier_lv_conf = soldierConf.getSoldierLvConf(soldier.getLv());

                // 获取下一等级配置
                next_soldier_lv_conf = soldierConf.getSoldierLvConf(soldier.getLv() + 1);

                if (!soldier_lv_conf) {
                    return next(null, {
                        code: Code.SOLDIER_LV_CONF_ERROR
                    });
                }

                if (!next_soldier_lv_conf) {
                    return next(null, {
                        code: Code.SOLDIER_LV_REACH_MAX
                    });
                }

                // 判断判断士兵等级是否大于士兵学院等级上限
                var soldier_college_build = build_model.getBuildByType(buildType.SOLDIERCOLLEGE);
                var build_conf = buildConf.getBuildLvConf(buildType.SOLDIERCOLLEGE, soldier_college_build.getLv());
                if (soldier.getLv() >= build_conf.soldier_lvl_limit) {
                    return next(null, {
                        code: Code.BUILD_SOLDIER_COLLEGE_LV_NOT_ENOUGH
                    });
                }

                // 用户扣除兵魂
                var module_type = moduleType.MDL_SOLDIER_COLLAGE;
                var operation_type = operationType.OPR_SOLDIER_COLLAGE_UP_LVL;
                if (next_soldier_lv_conf.soldier_soul_cost > 0) {
                    if (!roleService.subSoldierSoul(player_manager, module_type, operation_type, next_soldier_lv_conf.soldier_soul_cost)) {
                        return next(null, {
                            code: Code.USER_SOLDIER_SOUL_NOT_ENOUGH
                        });
                    }
                }

                // 用户扣除金币
                if (next_soldier_lv_conf.gold_cost > 0) {
                    if (!roleService.subGold(player_manager, module_type, operation_type, next_soldier_lv_conf.gold_cost)) {
                        return next(null, {
                            code: Code.USER_GOLD_NOT_ENOUGH
                        });
                    }
                }

                // 用户扣除粮食
                if (next_soldier_lv_conf.food_cost > 0) {
                    if (!roleService.subFood(player_manager, module_type, operation_type, next_soldier_lv_conf.food_cost)) {
                        return next(null, {
                            code: Code.USER_FOOD_NOT_ENOUGH
                        });
                    }
                }

                // 升级
                soldier_model.upgradeSoldier(soldier_type);

                // 更新战力
                soldierService.updateSoldierPower(player_manager);
                soldierService.refreshWallSoldierPower(player_manager);

                // 更新竞技场战力
                arenaService.uupdateArenaPower(player_manager, 1);
                arenaService.uupdateArenaPower(player_manager, 2);

                // 加入任务统计
                taskService.addTaskUpgradeSoldier(player_manager, soldier.getLv()); // 士兵最大等级统计
                taskService.addUpgradeSoldierNum(player_manager, soldier.getLv()); // 兵种升级次数统计

                // 统计同个等级的兵有多少个
                taskService.addSoldierLvlNumTotal(player_manager, soldier.getLv());
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
